#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import os
import unittest
import HTMLTestRunner
from tools import handle_request
from utili import LogConfigYaml
import logging
import logging.config
from ddt import *
import configs.config  # 加载配置
import tools.global_variable as glv

path = glv.get("rootpath")
data_path = os.path.join(path + '/datas/balancecheck.json')


@ddt
class GetBalanceCheck(unittest.TestCase):
    def setUp(self):
        self.run = handle_request.RequestHandler()
        LogConfigYaml.setup_logging()
        logger = logging.getLogger('server')  # 维护一个全局日志对象

    @file_data(data_path)
    def test_balance_check(self, method, url, params, expect):
        # params = {
        #     "tenantId": "aa"
        #
        # }

        res = self.run.visit(method, url, params=params)
        print(res.json())
        # self.assertEqual(expect, res.text)
        self.assertIn(expect, res.text)


if __name__ == '__main__':

    # unittest.main()
    report = os.path.join(path + '/outFiles/reports/result.html')
    print(report)
    file = open(report, "wb")
    # 创建测试套件
    testsuit = unittest.TestSuite()
    testload = unittest.TestLoader()
    # 括号内传入的是类名，会自动找到以test开头全部的用例
    # 用例以例表形式存储
    case = testload.loadTestsFromTestCase(GetBalanceCheck)
    testsuit.addTests(case)

    case_path = os.path.join(path + '/testCases')

    print(case_path)
    discover = unittest.defaultTestLoader.discover(case_path, pattern="*.py", top_level_dir=None)
    print(discover)

    run = HTMLTestRunner.HTMLTestRunner(stream=file,
                                        title="接口自动化测试报告",
                                        description="用例执行结果")
    run.run(testsuit)
    file.close()
